var itemno = "";
var applyid = "";
var nodeno = "";
var orgcode = "";
var nextnodecode1 = "";
var selectID = "userlist";
var applyno = "";

$(function(){
	applyno = getUrlParam("applyno");
	itemno = getUrlParam("itemno");
	
	$('#businessForm').load(itemno + "/detail.html");
	
	bindApply(applyno);

	
	$('#correction').hide();
	$('#edit').hide();
	$('#change').hide();
	$('.change').hide();
	$('#delete').hide();
	$('#delete').click(function(){
		
		$.confirm({
		    title: '提示!',
		    content: "删除数据就无法恢复，确定删除？",
		    confirmButton: '确定',
		    cancelButton: '取消',
		    confirm: function(){
				var param = {};
				param['applyid'] = applyid;
				param['isdetele'] = "1";
				
				$.ajax({ 
					url: projectPath + 'business/delete.do',
					data: param,
			        dataType: 'json',  
			        contentType:'application/json;charset=UTF-8', 
					success: function(data){
						if(data.result == "1"){
							$.alert('删除成功');
							window.close();
						}
					}
				});
		    }
		});
	});
})


function bindApply(applyno){
	$.ajax({ 
		url: basePath() + 'business/getapplyinfo.do',
		data: {"applyno": applyno},
		dataType: "json",
        contentType: "application/json",
		success: function(data){
	    	if(data.result == 1){
	    		var jsonApply = data.datas.apply;
	    		var jsonBusiness = data.datas.business;
	    		var jsonMaterial = data.datas.material;
	    		var jsonShip = data.datas.ship;
	    		var jsonCoordinate = data.datas.coordinate;


	    		applyid = jsonApply.applyid;
	    		
	    		bindApplyForm(jsonApply);
	    		
	    		//绑定业务资料
	    		bindBusinessForm(jsonBusiness);
	    		
	    		//绑定证书
	    		bindCertificate(jsonBusiness["recordcertificatelist"]);
	    	}
      	}
	});
}

function bindApplyForm(jsonApply){
	
	for(var d in jsonApply){//遍历json对象的每个key/value对,p为key
		if(d == 'applytime'){
			$('#' + d).html(formatDate(jsonApply[d], 'yyyy-mm-dd hh:mi'));
		}
		else if(d == 'nodename'){
        	if(jsonApply[d].indexOf('办结')>=0){
        		if(jsonApply[d].indexOf('不') >= 0)
        			$('#' + d).html('<span style="color:red;font-weight: bold;">'+ jsonApply[d] +'</span>');
        		else
        			$('#' + d).html('<span style="color:green;font-weight: bold;">'+ jsonApply[d] +'</span>');
        		

//        		//如果是水上水下业务，在审批通过后，可对这个业务单进行变更（新业务新流程）
//        		if(itemno == '1002'){
//        			$('#change').show();
//    				$('#change').attr('href', 'edit.html?applyno=' + applyno + '&itemno=' + itemno + '&change=1');
//        		}
        	}
			else if(jsonApply[d].indexOf('补正') >= 0){
				$('#' + d).html('<span style="color:red;font-weight: bold;">' + jsonApply[d] + '</span>');
				$('#correction').show();
				$('#correction').attr('href', 'correction.html?applyno=' + applyno + '&itemno=' + itemno);
			}
			else if(jsonApply[d].indexOf('申报') >= 0){
				$('#' + d).html('<span style="color:red;font-weight: bold;">未提交</span>');
				$('#edit').show();
				$('#delete').show();
				$('#edit').attr('href', 'recordEdit.html?applyno=' + applyno + '&itemno=' + itemno);
			}
        	else if(jsonApply[d].indexOf('受理') >= 0)
        		$('#' + d).html('<span style="color:blue; font-weight: bold;">待受理</span>');
			else{
				$('#' + d).html('<span style="color:blue;font-weight: bold;">已受理</span>');
			}
		}
		else{
			$('#' + d).html(jsonApply[d] == '' ? '/' : jsonApply[d]);
		}
	}
}

function bindBusinessForm(jsonBusiness){

	for ( var d in jsonBusiness) {// 遍历json对象的每个key/value对,p为key
		if (d == 'workbegintime' || d == 'workendtime'
				|| d == 'executionbegintime' || d == 'executionendtime'
				|| d == 'worktime' || d == 'workstarttime'
				|| d == 'ensurebegintime' || d == 'ensureendtime'
			    || d == 'delaydate' || d == 'eventtime') {
			$('#' + d).html(formatDate(jsonBusiness[d], 'yyyy-mm-dd'));
		} else if (d == 'waterwayname' || d == 'navigationmarkname') {
			$('#' + d).html(jsonBusiness[d] == '' ? '' : jsonBusiness[d] + ',');
		} else {
			$('#' + d).html(jsonBusiness[d] == '' ? '/' : jsonBusiness[d]);
		}
	}
}

function bindCertificate(recordcertificatelist){
	for(var i=0;i<recordcertificatelist.length;i++){
		var html='	<tr>'+
		  '		<td>'+
		  recordcertificatelist[i]["certificatetypename"]+
		  '		</td>'+
		  '		<td>'+
		  formatDate(recordcertificatelist[i]["issuedate"])	+
		  '		</td>'+
		  '		<td>'+
		  formatDate(recordcertificatelist[i]["effectivedate"])	+
		  '		</td>'+
		  '		<td id="filename'+ recordcertificatelist[i]["certificateid"]+'"></td>'+
		  '	</tr>';	
		$("#recordcertificate").find('tbody').append(html);
		
		for(var j=0;j<recordcertificatelist[i]["recordcertificatefile"].length;j++){
			var filestr='<span style="display:block;">'+recordcertificatelist[i]["recordcertificatefile"][j]["FILEURL"]+'</span>'
			$("#filename"+recordcertificatelist[i]["certificateid"]).append(filestr);
		}
	}
}